package cn.iocoder.yudao.module.picturebook.controller.admin.audioself;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.audioself.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.audioself.AudioSelfDO;
import cn.iocoder.yudao.module.picturebook.service.audioself.AudioSelfService;

@Tag(name = "管理后台 - 绘本自己录音")
@RestController
@RequestMapping("/picturebook/audio-self")
@Validated
public class AudioSelfController {

    @Resource
    private AudioSelfService audioSelfService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本自己录音")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:create')")
    public CommonResult<Integer> createAudioSelf(@Valid @RequestBody AudioSelfSaveReqVO createReqVO,@RequestHeader("token") Long userId) {
        return success(audioSelfService.createAudioSelf(createReqVO,userId));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本自己录音")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:update')")
    public CommonResult<Boolean> updateAudioSelf(@Valid @RequestBody AudioSelfSaveReqVO updateReqVO) {
        audioSelfService.updateAudioSelf(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本自己录音")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:delete')")
    public CommonResult<Boolean> deleteAudioSelf(@RequestParam("id") Integer id) {
        audioSelfService.deleteAudioSelf(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本自己录音")
                @PreAuthorize("@ss.hasPermission('picturebook:audio-self:delete')")
    public CommonResult<Boolean> deleteAudioSelfList(@RequestParam("ids") List<Integer> ids) {
        audioSelfService.deleteAudioSelfListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本自己录音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:query')")
    public CommonResult<AudioSelfRespVO> getAudioSelf(@RequestParam("id") Integer id) {
        AudioSelfDO audioSelf = audioSelfService.getAudioSelf(id);
        if (audioSelf == null) {
            return success(null);
        }
        return success(BeanUtils.toBean(audioSelf, AudioSelfRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本自己录音分页")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:query')")
    public CommonResult<PageResult<AudioSelfRecordRespVO>> getAudioSelfPage(@Valid AudioSelfPageReqVO pageReqVO,@RequestHeader("token") Long userId) {
        PageResult<AudioSelfRecordRespVO> pageResult = audioSelfService.getAudioSelfPage(pageReqVO, userId);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本自己录音 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:audio-self:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAudioSelfExcel(@Valid AudioSelfPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AudioSelfRecordRespVO> list = audioSelfService.getAudioSelfPage(pageReqVO, 0L).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本自己录音.xls", "数据", AudioSelfRespVO.class,
                        BeanUtils.toBean(list, AudioSelfRespVO.class));
    }

}